package com.cskaoyan.service.auth;

import com.alibaba.druid.util.StringUtils;
import com.cskaoyan.bean.MallAdmin;
import com.cskaoyan.bean.MallAdminExample;
import com.cskaoyan.mapper.MallAdminMapper;
import com.cskaoyan.mapper.MallPermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.x509.RFC822Name;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: project-market
 * @description:
 * @author: Weiwei Liu
 * @create: 2021-12-01 16:32
 **/
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    MallAdminMapper adminMapper;
    @Autowired
    MallPermissionMapper permissionMapper;

    @Override
    public boolean login(String username, String password) {

        MallAdminExample example = new MallAdminExample();

        example.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);

        List<MallAdmin> mallAdmins = adminMapper.selectByExample(example);

        // 用户不存在，登录失败
        return mallAdmins != null && mallAdmins.size() != 0;

    }

    @Override
    public String queryPasswordByUsername(String username) {

        return adminMapper.selectPasswordByUsername(username);

    }

    /**
     * 根据username查权限表
     *
     * @param username
     * @return
     */

    @Override
    public List queryPermissionByUsername(String username) {
        // 先根据admin表，username查role_ids数组，

        MallAdminExample example = new MallAdminExample();

        example.createCriteria().andUsernameEqualTo(username);

        List<MallAdmin> mallAdmins = adminMapper.selectByExample(example);

        Integer[] roleIds = mallAdmins.get(0).getRoleIds();

        // 再根据permission表的role_ids查权限表

        List<String> permissions = permissionMapper.selectPermissionByRoleIds(roleIds);

        return permissions;

    }
}
